<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>Picture.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="Picture code in Java">
<meta NAME="TITLE" CONTENT="Picture code in Java">
<meta NAME="KEYWORDS" CONTENT="Picture,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>Picture.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "Picture.java">Picture.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac Picture.java</span>
<span class="comment"> *  Execution:    java Picture imagename</span>
<span class="comment"> *  Dependencies: none</span>
<span class="comment"> *</span>
<span class="comment"> *  Data type for manipulating individual pixels of an image. The original</span>
<span class="comment"> *  image can be read from a file in jpg, gif, or png format, or the</span>
<span class="comment"> *  user can create a blank image of a given size. Includes methods for</span>
<span class="comment"> *  displaying the image in a window on the screen or saving to a file.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java Picture mandrill.jpg</span>
<span class="comment"> *</span>
<span class="comment"> *  Remarks</span>
<span class="comment"> *  -------</span>
<span class="comment"> *   - pixel (x, y) is column x and row y, where (0, 0) is upper left</span>
<span class="comment"> *</span>
<span class="comment"> *   - see also GrayPicture.java for a grayscale version</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Color</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">FileDialog</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Toolkit</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">ActionEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">ActionListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">KeyEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">BufferedImage</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">File</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">IOException</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">net</span><span class="symbol">.</span><span class="normal">URL</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">imageio</span><span class="symbol">.</span><span class="normal">ImageIO</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">ImageIcon</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JFrame</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JLabel</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenu</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenuBar</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenuItem</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">KeyStroke</span><span class="symbol">;</span>


<span class="comment">/**</span>
<span class="comment"> *  This class provides methods for manipulating individual pixels of</span>
<span class="comment"> *  an image. The original image can be read from a </span><span class="keyword">&lt;tt&gt;</span><span class="comment">.jpg</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">.gif</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment"> *  or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">.png</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> file or the user can create a blank image of a given size.</span>
<span class="comment"> *  This class includes methods for displaying the image in a window on</span>
<span class="comment"> *  the screen or saving it to a file.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Pixel (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) is column </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and row </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  By default, the origin (0, 0) is upper left, which is a common convention</span>
<span class="comment"> *  in image processing.</span>
<span class="comment"> *  The method </span><span class="keyword">&lt;tt&gt;</span><span class="comment">setOriginLowerLeft()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> change the origin to the lower left.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see</span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://introcs.cs.princeton.edu/31datatype"</span><span class="keyword">&gt;</span><span class="comment">Section 3.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Introduction to Programming in Java: An Interdisciplinary Approach</span><span class="keyword">&lt;/i&gt;</span>
<span class="comment"> *  by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Picture</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> ActionListener </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">BufferedImage</span><span class="normal"> image</span><span class="symbol">;</span><span class="normal">               </span><span class="comment">// the rasterized image</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">JFrame</span><span class="normal"> frame</span><span class="symbol">;</span><span class="normal">                      </span><span class="comment">// on-screen view</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">;</span><span class="normal">                   </span><span class="comment">// name of file</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> isOriginUpperLeft </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// location of origin</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">;</span><span class="normal">           </span><span class="comment">// width and height</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes a blank </span><span class="keyword">&lt;tt&gt;</span><span class="comment">width</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">-by-</span><span class="keyword">&lt;tt&gt;</span><span class="comment">height</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> picture, with </span><span class="keyword">&lt;tt&gt;</span><span class="comment">width</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> columns</span>
<span class="comment">     * and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">height</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> rows, where each pixel is black.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> width the width of the picture</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> height the height of the picture</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Picture</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> height</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">width  </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"width must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">height </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"height must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">width  </span><span class="symbol">=</span><span class="normal"> width</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">height </span><span class="symbol">=</span><span class="normal"> height</span><span class="symbol">;</span>
<span class="normal">        image </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> BufferedImage</span><span class="symbol">.</span><span class="normal">TYPE_INT_RGB</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// set to TYPE_INT_ARGB to support transparency</span>
<span class="normal">        filename </span><span class="symbol">=</span><span class="normal"> width </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"-by-"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> height</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes a new picture that is a deep copy of the argument picture.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> picture the picture to copy</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Picture</span><span class="symbol">(</span><span class="usertype">Picture</span><span class="normal"> picture</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        width  </span><span class="symbol">=</span><span class="normal"> picture</span><span class="symbol">.</span><span class="function">width</span><span class="symbol">();</span>
<span class="normal">        height </span><span class="symbol">=</span><span class="normal"> picture</span><span class="symbol">.</span><span class="function">height</span><span class="symbol">();</span>
<span class="normal">        image </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> BufferedImage</span><span class="symbol">.</span><span class="normal">TYPE_INT_RGB</span><span class="symbol">);</span>
<span class="normal">        filename </span><span class="symbol">=</span><span class="normal"> picture</span><span class="symbol">.</span><span class="normal">filename</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> col </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> col </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="function">width</span><span class="symbol">();</span><span class="normal"> col</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> row </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> row </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="function">height</span><span class="symbol">();</span><span class="normal"> row</span><span class="symbol">++)</span>
<span class="normal">                image</span><span class="symbol">.</span><span class="function">setRGB</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> row</span><span class="symbol">,</span><span class="normal"> picture</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> row</span><span class="symbol">).</span><span class="function">getRGB</span><span class="symbol">());</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes a picture by reading from a file or URL.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> filename the name of the file (.png, .gif, or .jpg) or URL.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Picture</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">filename </span><span class="symbol">=</span><span class="normal"> filename</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">// try to read from file in working directory</span>
<span class="normal">            </span><span class="usertype">File</span><span class="normal"> file </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">File</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">file</span><span class="symbol">.</span><span class="function">isFile</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                image </span><span class="symbol">=</span><span class="normal"> ImageIO</span><span class="symbol">.</span><span class="function">read</span><span class="symbol">(</span><span class="normal">file</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// now try to read from file in same directory as this .class file</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getClass</span><span class="symbol">().</span><span class="function">getResource</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">url </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    url </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">URL</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                image </span><span class="symbol">=</span><span class="normal"> ImageIO</span><span class="symbol">.</span><span class="function">read</span><span class="symbol">(</span><span class="normal">url</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">image </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Invalid image file: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            width  </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getWidth</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">            height </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getHeight</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="comment">// e.printStackTrace();</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">RuntimeException</span><span class="symbol">(</span><span class="string">"Could not open file: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes a picture by reading in a .png, .gif, or .jpg from a file.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> file the file</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Picture</span><span class="symbol">(</span><span class="usertype">File</span><span class="normal"> file</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            image </span><span class="symbol">=</span><span class="normal"> ImageIO</span><span class="symbol">.</span><span class="function">read</span><span class="symbol">(</span><span class="normal">file</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            e</span><span class="symbol">.</span><span class="function">printStackTrace</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">RuntimeException</span><span class="symbol">(</span><span class="string">"Could not open file: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">image </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">RuntimeException</span><span class="symbol">(</span><span class="string">"Invalid image file: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        width  </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getWidth</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        height </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getHeight</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        filename </span><span class="symbol">=</span><span class="normal"> file</span><span class="symbol">.</span><span class="function">getName</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns a JLabel containing this picture, for embedding in a JPanel,</span>
<span class="comment">     * JFrame or other GUI widget.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">JLabel</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">JLabel</span><span class="normal"> </span><span class="function">getJLabel</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">image </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span><span class="normal">         </span><span class="comment">// no image available</span>
<span class="normal">        </span><span class="usertype">ImageIcon</span><span class="normal"> icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JLabel</span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Sets the origin to be the upper left pixel. This is the default.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setOriginUpperLeft</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        isOriginUpperLeft </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Sets the origin to be the lower left pixel.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setOriginLowerLeft</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        isOriginUpperLeft </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Displays the picture in a window on the screen.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">show</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// create the GUI for viewing the image if needed</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">frame </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            frame </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JFrame</span><span class="symbol">();</span>

<span class="normal">            </span><span class="usertype">JMenuBar</span><span class="normal"> menuBar </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenuBar</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">JMenu</span><span class="normal"> menu </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenu</span><span class="symbol">(</span><span class="string">"File"</span><span class="symbol">);</span>
<span class="normal">            menuBar</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">menu</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">JMenuItem</span><span class="normal"> menuItem1 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenuItem</span><span class="symbol">(</span><span class="string">" Save...   "</span><span class="symbol">);</span>
<span class="normal">            menuItem1</span><span class="symbol">.</span><span class="function">addActionListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>
<span class="normal">            menuItem1</span><span class="symbol">.</span><span class="function">setAccelerator</span><span class="symbol">(</span><span class="normal">KeyStroke</span><span class="symbol">.</span><span class="function">getKeyStroke</span><span class="symbol">(</span><span class="normal">KeyEvent</span><span class="symbol">.</span><span class="normal">VK_S</span><span class="symbol">,</span>
<span class="normal">                                     Toolkit</span><span class="symbol">.</span><span class="function">getDefaultToolkit</span><span class="symbol">().</span><span class="function">getMenuShortcutKeyMask</span><span class="symbol">()));</span>
<span class="normal">            menu</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">menuItem1</span><span class="symbol">);</span>
<span class="normal">            frame</span><span class="symbol">.</span><span class="function">setJMenuBar</span><span class="symbol">(</span><span class="normal">menuBar</span><span class="symbol">);</span>



<span class="normal">            frame</span><span class="symbol">.</span><span class="function">setContentPane</span><span class="symbol">(</span><span class="function">getJLabel</span><span class="symbol">());</span>
<span class="normal">            </span><span class="comment">// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span>
<span class="normal">            frame</span><span class="symbol">.</span><span class="function">setDefaultCloseOperation</span><span class="symbol">(</span><span class="normal">JFrame</span><span class="symbol">.</span><span class="normal">DISPOSE_ON_CLOSE</span><span class="symbol">);</span>
<span class="normal">            frame</span><span class="symbol">.</span><span class="function">setTitle</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">            frame</span><span class="symbol">.</span><span class="function">setResizable</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">            frame</span><span class="symbol">.</span><span class="function">pack</span><span class="symbol">();</span>
<span class="normal">            frame</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// draw</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">repaint</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the height of the picture.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the height of the picture (in pixels)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">height</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> height</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the width of the picture.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the width of the picture (in pixels)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">width</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> width</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the color of pixel (</span><span class="keyword">&lt;tt&gt;</span><span class="comment">col</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">row</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> col the column index</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> row the row index</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the color of pixel (</span><span class="keyword">&lt;tt&gt;</span><span class="comment">col</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">row</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless both 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">col</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">width</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *         and 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">row</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">height</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> col</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> row</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">col </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> col </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="function">width</span><span class="symbol">())</span><span class="normal">  </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"col must be between 0 and "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="function">width</span><span class="symbol">()-</span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">row </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> row </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="function">height</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"row must be between 0 and "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="function">height</span><span class="symbol">()-</span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">isOriginUpperLeft</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">.</span><span class="function">getRGB</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> row</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">                   </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">.</span><span class="function">getRGB</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> height </span><span class="symbol">-</span><span class="normal"> row </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Sets the color of pixel (</span><span class="keyword">&lt;tt&gt;</span><span class="comment">col</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">row</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">) to given color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> col the column index</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> row the row index</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> color the color</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless both 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">col</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">width</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *         and 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">row</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;lt;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">height</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">color</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">set</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> col</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> row</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> color</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">col </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> col </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="function">width</span><span class="symbol">())</span><span class="normal">  </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"col must be between 0 and "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="function">width</span><span class="symbol">()-</span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">row </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> row </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="function">height</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"row must be between 0 and "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="function">height</span><span class="symbol">()-</span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">color </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"can't set Color to null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">isOriginUpperLeft</span><span class="symbol">)</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">setRGB</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> row</span><span class="symbol">,</span><span class="normal"> color</span><span class="symbol">.</span><span class="function">getRGB</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">                   image</span><span class="symbol">.</span><span class="function">setRGB</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> height </span><span class="symbol">-</span><span class="normal"> row </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> color</span><span class="symbol">.</span><span class="function">getRGB</span><span class="symbol">());</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns true if this picture is equal to the argument picture.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> other the other picture</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if this picture is the same dimension as </span><span class="keyword">&lt;tt&gt;</span><span class="comment">other</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *         and if all pixels have the same color; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">equals</span><span class="symbol">(</span><span class="usertype">Object</span><span class="normal"> other</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">other </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">other </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">other</span><span class="symbol">.</span><span class="function">getClass</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">getClass</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Picture</span><span class="normal"> that </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Picture</span><span class="symbol">)</span><span class="normal"> other</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">width</span><span class="symbol">()</span><span class="normal">  </span><span class="symbol">!=</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">width</span><span class="symbol">())</span><span class="normal">  </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">height</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">height</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> col </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> col </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="function">width</span><span class="symbol">();</span><span class="normal"> col</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> row </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> row </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="function">height</span><span class="symbol">();</span><span class="normal"> row</span><span class="symbol">++)</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> row</span><span class="symbol">).</span><span class="function">equals</span><span class="symbol">(</span><span class="normal">that</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">col</span><span class="symbol">,</span><span class="normal"> row</span><span class="symbol">)))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This operation is not supported because pictures are mutable.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> does not return a value</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> UnsupportedOperationException if called</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">hashCode</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">UnsupportedOperationException</span><span class="symbol">(</span><span class="string">"hashCode() is not supported because pictures are mutable"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Saves the picture to a file in a standard image format.</span>
<span class="comment">     * The filetype must be .png or .jpg.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> name the name of the file</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">save</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> name</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">save</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">File</span><span class="symbol">(</span><span class="normal">name</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Saves the picture to a file in a PNG or JPEG image format.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> file the file</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">save</span><span class="symbol">(</span><span class="usertype">File</span><span class="normal"> file</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        filename </span><span class="symbol">=</span><span class="normal"> file</span><span class="symbol">.</span><span class="function">getName</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">frame </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> frame</span><span class="symbol">.</span><span class="function">setTitle</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> suffix </span><span class="symbol">=</span><span class="normal"> filename</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">.</span><span class="function">lastIndexOf</span><span class="symbol">(</span><span class="string">'.'</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">        suffix </span><span class="symbol">=</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">toLowerCase</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">suffix</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"jpg"</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> suffix</span><span class="symbol">.</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"png"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                ImageIO</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> suffix</span><span class="symbol">,</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                e</span><span class="symbol">.</span><span class="function">printStackTrace</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Error: filename must end in .jpg or .png"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Opens a save dialog box when the user selects "Save As" from the menu.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">actionPerformed</span><span class="symbol">(</span><span class="usertype">ActionEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">FileDialog</span><span class="normal"> chooser </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">FileDialog</span><span class="symbol">(</span><span class="normal">frame</span><span class="symbol">,</span>
<span class="normal">                             </span><span class="string">"Use a .png or .jpg extension"</span><span class="symbol">,</span><span class="normal"> FileDialog</span><span class="symbol">.</span><span class="normal">SAVE</span><span class="symbol">);</span>
<span class="normal">        chooser</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">chooser</span><span class="symbol">.</span><span class="function">getFile</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">save</span><span class="symbol">(</span><span class="normal">chooser</span><span class="symbol">.</span><span class="function">getDirectory</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> File</span><span class="symbol">.</span><span class="normal">separator </span><span class="symbol">+</span><span class="normal"> chooser</span><span class="symbol">.</span><span class="function">getFile</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Unit tests this </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Picture</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     * Reads a picture specified by the command-line argument,</span>
<span class="comment">     * and shows it in a window on the screen.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Picture</span><span class="normal"> picture </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Picture</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d-by-%d</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> picture</span><span class="symbol">.</span><span class="function">width</span><span class="symbol">(),</span><span class="normal"> picture</span><span class="symbol">.</span><span class="function">height</span><span class="symbol">());</span>
<span class="normal">        picture</span><span class="symbol">.</span><span class="function">show</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>


<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
